package com.pine.dcts.auth.service;




import com.pine.app.module.security.core.userdetails.UserDetails;
import com.pine.app.module.security.oauth.user.IntegrationLoginRequest;
import com.pine.app.module.security.oauth.user.authenticator.UsernamePasswordAuthenticator;
import com.pine.dcts.common.DctsException;
import com.pine.dcts.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;


/**
 * 默认登录处理
 *
 * @author XYJXUST
 * @date 2018-3-31
 **/
@Component
@Primary
@Slf4j
public class UsernamePasswordAuthenticatorImpl extends UsernamePasswordAuthenticator {

    @Autowired
    private UserService userAPI;



    @Override
    public UserDetails authenticate(IntegrationLoginRequest integrationAuthentication) {
        if(StringUtils.isEmpty(integrationAuthentication.getUsername())
                || StringUtils.isEmpty(integrationAuthentication.getAuthParameter("password"))){
            throw new DctsException("账号或者密码不能为空");
        }
        return userAPI.getByLoginName(integrationAuthentication.getUsername());
    }





}
